package com.rftennis.service.impl;


import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

import javax.security.auth.login.LoginException;

import org.apache.ibatis.session.SqlSession;

import com.rftennis.dao.SqlSessionUtil;
import com.rftennis.dao.SystemAdminDao;
import com.rftennis.dto.SystemAdminContext;
import com.rftennis.entity.SystemAdminEntity;
import com.rftennis.exception.RftennisException;

import com.rftennis.service.SystemAdminService;


public class SystemAdminServiceImpl implements SystemAdminService {

	
    //实现登陆方法
	public SystemAdminContext login(String account, String password) throws Exception {
		
		
		//1验证参数
		//1.1 都不能为空(null、空字符串、空白字符串)
				account=assertNotBlank("account不能为空", account);
				password=assertNotBlank("password不能为空", password);
				
				//1.2 长度约束
				assertLessThan("account非法", account, 32);
				assertLessThan("password非法", password, 16);
				//1.3 其他约束(省略)
		//2执行逻辑
				SqlSession session=SqlSessionUtil.getSession();
				SystemAdminDao adminDao=session.getMapper(SystemAdminDao.class);
				
				SystemAdminEntity adminEntity=adminDao.selectByAccount(account);
				if(adminEntity==null) {
					throw new RftennisException("account-and-password.not-match","账号不存在");
				}
				//2.2 判断密码是否正确
				if(!password.equals(adminEntity.getPassword())) {//注意顺序
					throw new RftennisException("account-and-password.not-match","账号密码不匹配");
				}
				
				//2.3 更新最后登录时间
				Map<String, Object> map=new HashMap<String, Object>();
				map.put("lastLoginTime",LocalDateTime.now());
				
				
				adminDao.update(adminEntity.getId(),map);
				
				//3 组装结果
				SystemAdminContext ac=new SystemAdminContext();
				ac.setId(adminEntity.getId());
				ac.setAccount(adminEntity.getAccount());
				ac.setName(adminEntity.getName());
				ac.setCreateTime(adminEntity.getCreateTime());
				ac.setLastLoginTime(adminEntity.getLastLoginTime());
				return ac;
		


	}
	
	//实现修改密码方法
	public void updatePassword(String id, String oldPassword, String newPasswrod, String newPasswordConfirm)throws Exception {
		//1验证参数
		//2执行逻辑
	   //3组装结果
		
	}
	
}
